<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>制定物料设计单页面</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
    <link href="/css/style.css?v=4.1.0" rel="stylesheet">
    <link href="/bootstraptable/bootstrap-table.css" rel="stylesheet">
    <style>
        .text_bottom_border {
            border: none;
            border-bottom: 1px solid black;
        }

        .text_no_border {
            border: none;
        }
    </style>
</head>
<body>
<h5 style="color: #3e79b6">您正在做的业务是：产品设计--产品物料组成设计--制定物料组成设计单</h5>
<div class="container" style="margin-top: 20px;">
<div style="text-align: right" id="material_div">
    <button type="button" id="add_btn" class="btn btn-default">添加物料</button>
    <button type="button" id="remove_btn" class="btn btn-default">删除物料</button>
    <button type="button" id="preview_btn" class="btn btn-default">预览</button>
    <button type="button" id="submit_btn" class="btn btn-default">确定</button>
    <button type="button" class="btn btn-default" onclick="history.back();">返回</button>
</div>
    <div class="panel panel-default" style="margin-top: 10px;">
        <form id="material_form" class="form-horizontal">
            <div class="panel-heading">
                <div class="panel-title">
                    <h2 style="text-align: center;">物料组成设计单</h2>
                </div>
            </div>
            <div class="panel-body">
                <div class="form-group">
                    <div class="col-sm-6">
                        <label class="col-sm-4 control-label">产品名称：</label>
                        <div class="col-sm-8">
                            <input type="text" name="productName" id="productName"
                                   class="form-control text_no_border"
                                   style="background-color: white"
                                   readonly>
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <label class="col-sm-4 control-label">产品编号：</label>
                        <div class="col-sm-8">
                            <input type="text" name="productId" id="productId"
                                   class="form-control text_no_border"
                                   style="background-color: white"
                                   readonly>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-6">
                        <label class="col-sm-4 control-label">设计人：</label>
                        <div class="col-sm-8">
                            <input type="text" name="designer" id="designer"
                                   class="form-control text_bottom_border"
                            >
                        </div>
                    </div>
                </div>
                <!--物料明细表格-->
                <table class="table table-bordered table-striped" data-toggle="table" id="material_details_table">
                   <thead>
                        <tr>
                            <th data-field="detailsNumber" data-formatter="formatChk">点选</th>
                            <th data-field="productId">物料编号</th>
                            <th data-field="productName">物料名称</th>
                            <th data-field="type" data-formatter="formatType">用途类型</th>
                            <th data-field="productDescribe">描述</th>
                            <th data-field="amount" data-formatter="formatText">数量</th>
                            <th data-field="amountUnit">单位</th>
                            <th data-field="costPrice">计划成本单价（元）</th>
                            <th data-field="subtotal">小计（元）</th>
                        </tr>
                   </thead>
                </table>

                <div class="form-group">
                    <div class="col-sm-6">
                        <label class="col-sm-4 control-label">登记人：</label>
                        <div class="col-sm-8">
                            <input type="text" name="register" id="register"
                                   class="form-control text_bottom_border"
                            >
                        </div>
                    </div>
                    <div class="col-sm-6">
                        <label class="col-sm-4 control-label">登记时间：</label>
                        <div class="col-sm-8">
                            <input type="text" name="registerTime" id="registerTime" class="form-control"
                                   style="border: none;background-color: white" readonly/>
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-8">
                        <label class="col-sm-3 control-label">设计要求：</label>
                        <div class="col-sm-9">
                            <textarea rows="3" cols="100" class="form-control"
                                      style="resize: none;overflow-y: scroll" name="moduleDescribe" id="moduleDescribe"></textarea>
                        </div>
                    </div>
                </div>

            </div>
        </form>
    </div>


    <!--显示添加物料的模态框，查询所有的物料信息-->
    <div class="modal fade "  id="material_modal">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                        <span>&times;</span>
                    </button>
                </div>
                <div class="modal-body">

                    <!--查询条件面板-->
                    <div id="material_file_search_div" class="panel panel-default">
                        <div class="panel-heading">
                            <h3>查询条件</h3>
                        </div>
                        <div class="panel-body">
                            <form class="form-inline">
                                <div class="form-group">
                                    <label>请选择I级分类：</label>
                                    <select class="form-control" id="search_firstKindId" name="search_firstKindId">
                                        <option value="">--请选择--</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>请选择II级分类：</label>
                                    <select class="form-control" id="search_secondKindId" name="search_secondKindId">
                                        <option value="">--请选择--</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>请选择III级分类：</label>
                                    <select class="form-control" id="search_thirdKindId" name="search_thirdKindId">
                                        <option value="">--请选择--</option>
                                    </select>
                                </div>
                                <button type="button" id="search_btn" class="btn btn-default col-md-offset-1" style="text-align: right">查询</button>
                            </form>
                        </div>

                    </div>

                    <!--查询数据表格-->
                    <table id="material_file_list_table"></table>

                </div>
                <div class="modal-footer">
                    <button type="button" id="material_back_btn" class="btn btn-default col-md-2 col-md-offset-10">返回
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>


<!-- 全局js -->
<script src="/js/jquery.min.js?v=2.1.4"></script>
<script src="/js/bootstrap.min.js?v=3.3.6"></script>
<script src="/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="/js/plugins/layer/layer.min.js"></script>

<!-- 自定义js -->
<script src="/js/hAdmin.js?v=4.1.0"></script>
<script src="/js/currentTime.js"></script>

<!-- 第三方插件 -->
<script src="/js/plugins/pace/pace.min.js"></script>

<!--BootstrapTable-->
<script src="/bootstraptable/bootstrap-table.min.js"></script>
<script src="/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>


<script>
    $(function (){
        //登记时间
        $("#material_form #registerTime").val(add(fortime()))

        //获取产品档案id
        let id = localStorage.getItem("id")
        $.post("/design/file/getMaterialOne", {"id": id}, function (res) {
            $("#material_form #productName").val(res.productName)
            $("#material_form #register").val(res.register)
            $("#material_form #productId").val(res.productId)
        })

        $("#material_modal").modal({
            backdrop: false,
            keyboard: false,
            show: false
        })

        //点击添加物料按钮
        $("#material_div #add_btn").click(function () {
            //弹出模态框
            $("#material_modal").modal("show")


            //I级分类
            firstKind()
            //II级分类
            secondKind()
            //III级分类
            thirdKind()


            //发送查看物料的请求
            $("#material_file_list_table").bootstrapTable({
                method: "post",
                contentType: "application/x-www-form-urlencoded",
                url: "/design/file/makeMaterialList",
                // 数据行交替样式
                striped: true,
                // 是否是服务器端分页：server表示服务器端，client表示前端分页
                sidePagination: 'server',
                //初始化加载第一页
                pageNumber: 1,
                //是否分页
                pagination: true,
                // 单页记录数，页大小
                pageSize: 5,
                // 分页列表
                pageList: [8, 10, 20],
                //刷新按钮
                // showRefresh:true,
                // 请求参数
                queryParams: function (params) {
                    let obj = {
                        // params.limit获取组件中的页大小
                        // params.offset获取请求数据的起始索引
                        pageSize: params.limit,
                        pageNo: (params.offset / params.limit) + 1,

                        // 查询表单的数据
                        search_firstKindId: $("#material_file_search_div #search_firstKindId").val(),
                        search_secondKindId: $("#material_file_search_div #search_secondKindId").val(),
                        search_thirdKindId: $("#material_file_search_div #search_thirdKindId").val()
                    }
                    return obj;
                },
                // 表格列标题
                columns: [{
                    title: "物料编号",
                    field: "productId",
                    formatter: function (value, row, index) {
                        let html = `<a href="#">${value}</a>`
                        return html
                    }
                }, {
                    title: "物料名称",
                    field: "productName"
                }, {
                    title: "用途类型",
                    field: "type",
                    formatter: function (value, row, index) {
                        if (row.type === "Y001-1")
                            return "商品"
                        else
                            return "物料"
                    }
                }, {
                    title: "物料描述",
                    field: "productDescribe"
                }, {
                    title: "单位",
                    field: "amountUnit"
                }, {
                    title: "计划成本单价",
                    field: "costPrice"
                }, {
                    title: "添加",
                    formatter: function (value, row, index) {
                        // js对象转化成json字符串
                        let json = JSON.stringify(row)
                        let html = "<a href='javascript:addModuleDetails(" + json +")'>添加</a>"
                        return html
                    }
                }]
            })

            //查询按钮
            $("#material_file_search_div search_btn").click(function () {
                $("#material_file_list_table").bootstrapTable("refresh")
            })
        })

        //点击物料删除按钮
        $("#material_div #remove_btn").click(function (){
            //获取所有选中的复选框
            let check_ary=$("#material_form #material_details_table :checkbox[name='detailsNumber']:checked")
            //如果没有选中复选框
            if(check_ary.length==0){
                alert("请先选中要删除的物料明细")
                return
            }
            //选中复选框
            //获取所有复选框对象
            let checks=$("#material_form #material_details_table :checkbox[name='detailsNumber']")
            //获取所有数据行
            let data=$("#material_form #material_details_table").bootstrapTable("getData")
            for(let i=checks.length-1;i>=0;i--){
                //判断是复选框否选中
                if(checks[i].checked) {
                    // 先删除data数组对应的对象
                    data.splice(i, 1)
                    // 再删除表格的tr
                    $(checks[i]).parent("td").parent().remove()
                }
            }
        })

        //点击预览按钮
        $("#material_div #preview_btn").click(function (){
            //获取所有数据行
            let data=$("#material_form #material_details_table").bootstrapTable("getData")
            //获取所有复选框对象
            let checks=$("#material_form #material_details_table :checkbox[name='detailsNumber']")
            // 获取所有的数量文本框
            let amounts = $("#material_form #material_details_table input[name='amount']")
            for (let i=0; i<data.length; i++) {
                // 计算明细小计
                data[i].subtotal = data[i].costPrice * data[i].amount
                console.log("小计",data[i].subtotal)
                // // 更新小计单元格
                $(checks[i]).parent("td").siblings(":last").html(data[i].subtotal)

                // 更新点选复选框所在的单元格
                $(checks[i]).parent("td").html((i+1))
                // 更新数量文本框所在的单元格
                $(amounts[i]).parent("td").html($(amounts[i]).val())

                $("#material_div #add_btn").css("display","none")
                $("#material_div #remove_btn").css("display","none")
            }

        })

        //点击确定按钮
        $("#material_div #submit_btn").click(function (){
            let module_details=$("#material_form #material_details_table").bootstrapTable("getData")

            let module_dto={
                "productId":$("#material_form #productId").val(),
                "productName":$("#material_form #productName").val(),
                "designer": $("#material_form #designer").val(),
                "register": $("#material_form #register").val(),
                "registerTime": $("#material_form #registerTime").val(),
                "moduleDescribe":$("#material_form #moduleDescribe").val()
            }
            module_dto.details=module_details
            //利用原生Ajax发送请求
            $.ajax({
                url: "/design/module/addModule",
                type:"post",
                data: JSON.stringify(module_dto),
                contentType: "application/json",
                success: function (data) {
                    if(data){
                        window.location="design_material_unmake_listUI"
                    }
                }
            })
        })


        //点击返回按钮，关闭模态框
        $("#material_back_btn").click(function () {
            $("#material_modal").modal("hide")
        })

    })





    function firstKind() {
        $.post("/design/file/select", {"kid": "1"}, function (res) {
            $("#material_file_search_div #search_firstKindId option:gt(0)").remove()
            for (let i = 0; i < res.length; i++) {
                let obj = res[i]
                let $op = $("<option>" + obj.kindName + "</option>")
                $op.val(obj.kindId)
                $("#search_firstKindId").append($op)
            }
        })
    }

    function secondKind() {
        $.post("/design/file/select", {"kid": "2"}, function (res) {
            $("#material_file_search_div #search_secondKindId option:gt(0)").remove()
            for (let i = 0; i < res.length; i++) {
                let obj = res[i]
                let $op = $("<option>" + obj.kindName + "</option>")
                $op.val(obj.kindId)
                $("#search_secondKindId").append($op)
            }
        })
    }

    function thirdKind() {
        $.post("/design/file/select", {"kid": "3"}, function (res) {
            $("#material_file_search_div #search_thirdKindId option:gt(0)").remove()
            for (let i = 0; i < res.length; i++) {
                let obj = res[i]
                let $op = $("<option>" + obj.kindName + "</option>")
                $op.val(obj.kindId)
                $("#search_thirdKindId").append($op)
            }
        })
    }

    // 添加物料明细
    function addModuleDetails(module) {

        // 获取物料明细表格的所有数据
        let data = $("#material_form #material_details_table").bootstrapTable("getData")

        // 标志位
        let index = -1
        for (let i = 0; i<data.length; i++) {
            if(module.productId === data[i].productId) {
                index = i
                break
            }
        }
        if(index === -1) {
            // 设置添加对象的数量属性
            module.amount = 1
            // 向物料明细表格中添加一行
            $("#material_form #material_details_table").bootstrapTable('append', module)

        } else {
            // 获取所有的数量文本框
            let amounts = $("#material_form #material_details_table input[name='amount']")
            // 文本框数量 + 1
            let count =parseInt($(amounts[index]).val())
            count ++
            $(amounts[index]).val(count)
            // 修改对应对象的amount属性
            data[index].amount = count
        }

    }




    // 格式化"用途类型"单元格
    function formatType(value, row, index) {
        return row.type == 'Y001-1'? '商品' : '物料'
    }

    // 格式化"点选"单元格
    function formatChk(value, row, index) {
        return "<input type='checkbox' name='detailsNumber'>"
    }

    // 格式化"数量"文本框
    function formatText(value, row, index) {
        return `<input type='text' class='form-control' name='amount' value='${row.amount}'
                    onblur="changeAmount(this)">`
    }

    // "数量"文本框失去焦点的事件
    function changeAmount(param) {
        // 文本框的数量
        let val = $(param).val()
        // 获取文本框所在的行索引
        let index = $(param).parent("td").parent().index()

        // 获取物料明细表格的所有数据
        let data = $("#material_form #material_details_table").bootstrapTable("getData")
        // 修改对应对象的amount
        data[index].amount = parseInt(val)
        console.log(data)
    }

</script>
</body>
</html>